package cn.hrc.controller.adm;

import cn.hrc.entity.Book;
import cn.hrc.entity.TypeInfo;
import cn.hrc.entity.XJson;
import cn.hrc.service.impl.BookServiceImpl;
import cn.hrc.service.impl.FileServiceImpl;
import cn.hrc.service.impl.TypeServiceImpl;
import cn.hrc.utils.PackJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/adm/book")
public class BookController {
    @Autowired
    BookServiceImpl bookService;
    @Autowired
    TypeServiceImpl typeService;
    @Autowired
    FileServiceImpl fileService;
    @RequestMapping("/findAll")
    public XJson findAll() {
        List<Book> books = bookService.findAll();
        return PackJson.pack(200, "ok", books);
    }
    @GetMapping("/findById/{id}")
    public XJson findById(@PathVariable("id") int id) {
        // 获取商品
        Book book = bookService.findById(id);
        return PackJson.pack(200, "ok", book);
    }
    @RequestMapping("/addBook")
    public XJson addBook(@RequestParam(value="myPic", required = false) MultipartFile myFile,
                         Book book) {
        String uuid = null;
        if (myFile != null && myFile.getSize() > 0){
            uuid = fileService.addFile(myFile);
        }
        book.setPic(uuid);
        int n = bookService.addBook(book);
        return PackJson.pack(200, "ok", n);
    }
    @RequestMapping("/updBook")
    public XJson updateBook(@RequestParam(value="myPic", required = false) MultipartFile myFile,
                            Book book) {
        String uuid = null;
        if (myFile != null && myFile.getSize() > 0){
            uuid = fileService.updFile(myFile,book.getPic());
        }
        book.setPic(uuid);
        int n = bookService.updateBook(book);
        return PackJson.pack(200, "ok", n);
    }
}
